// HeavenII Library
// Copyright (C) HeavenII Team. All rights reserved.
// 
// This file is a part of the HeavenII Library.
// The use and distribution terms for this software are covered
// by the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// which can be found in the file LICENSE at the root folder.

#pragma once

namespace HeavenII
{
namespace datetime
{

inline size_t FormatTimestamp(wchar_t *pBuffer, size_t sizeInWords, const wchar_t *pszFormat, time_t timestamp)
{
  struct tm _Tm;
  localtime_s(&_Tm, &timestamp);
  return wcsftime(pBuffer, sizeInWords, pszFormat, &_Tm);
}


inline size_t FormatTimestamp(char *pBuffer, size_t sizeInBytes, const char *pszFormat, time_t timestamp)
{
  struct tm _Tm;
  localtime_s(&_Tm, &timestamp);
  return strftime(pBuffer, sizeInBytes, pszFormat, &_Tm);
}


inline time_t TimestampFromText(const char *pszDatetime, const char *pszFmt)
{
    struct tm _tm;
    memset(&_tm, 0, sizeof(struct tm));
    sscanf_s(pszDatetime, pszFmt, &_tm.tm_year, &_tm.tm_mon, &_tm.tm_mday, &_tm.tm_hour, &_tm.tm_min, &_tm.tm_sec);
    _tm.tm_year -= 1900;
    _tm.tm_mon -= 1;
    return mktime(&_tm);
}


inline time_t Today()
{
    time_t t = time(0);
    struct tm _tm;
    localtime_s(&_tm, &t);
    _tm.tm_hour = 0;
    _tm.tm_min = 0;
    _tm.tm_sec = 0;
    return mktime(&_tm);
}

}
}
